const mongoose = require("mongoose");

const defaultAvatar =
  "http://192.168.83.134:9001/picture/%E3%80%90%E5%93%B2%E9%A3%8E%E5%A3%81%E7%BA%B8%E3%80%912024-10-24%2020_08_11.png";

const UserSchema = new mongoose.Schema({
  username: {
    type: String,
    trim: true,
    default: "未命名用户", // 默认用户名
  },
  tags: {
    type: [String],
    default: [], // 默认标签为空数组
  },
  phone: {
    type: String,
    unique: true,
    trim: true,
  },
  passwordHash: {
    type: String,
    required: true, // 必填项
  },
  avatar: {
    type: String,
    default: defaultAvatar, // 默认头像
  },
});

// 删除敏感信息（如密码、_id 等）
UserSchema.set("toJSON", {
  transform(doc, ret) {
    ret.id = ret._id;
    delete ret._id;
    delete ret.__v;
    delete ret.passwordHash;
    return ret;
  },
});

module.exports = mongoose.model("User", UserSchema);
